# warehouse.py  仓库表

# 仓库表结构定义
# CREATE TABLE warehouses (
#     id INT PRIMARY KEY AUTO_INCREMENT,  # 仓库ID，主键自增
#     name VARCHAR(100) NOT NULL,  # 仓库名称，必填
#     location VARCHAR(200),  # 仓库位置，可选
#     created_by INT,  # 创建者ID
#     FOREIGN KEY (created_by) REFERENCES admin_users(id)  # 外键关联管理员表
# );

# 物品表结构定义 
# CREATE TABLE items (
#     id INT PRIMARY KEY AUTO_INCREMENT,  # 物品ID，主键自增
#     name VARCHAR(100) NOT NULL,  # 物品名称，必填
#     description TEXT,  # 物品详细描述，可选
#     created_by INT,  # 创建者ID
#     FOREIGN KEY (created_by) REFERENCES admin_users(id)  # 外键关联管理员表
# );

# 库存记录表结构定义
# CREATE TABLE inventory (
#     id INT PRIMARY KEY AUTO_INCREMENT,  # 记录ID，主键自增
#     warehouse_id INT,  # 仓库ID
#     item_id INT,  # 物品ID
#     quantity INT NOT NULL,  # 物品数量，必填
#     status ENUM('pending','approved','rejected') DEFAULT 'pending',  # 审核状态，默认待审核
#     created_by INT,  # 创建者ID
#     created_at DATETIME DEFAULT CURRENT_TIMESTAMP,  # 创建时间，默认当前时间
#     FOREIGN KEY (warehouse_id) REFERENCES warehouses(id),  # 外键关联仓库表
#     FOREIGN KEY (item_id) REFERENCES items(id),  # 外键关联物品表
#     FOREIGN KEY (created_by) REFERENCES admin_users(id)  # 外键关联管理员表
# );

# 审核记录表结构定义
# CREATE TABLE audit_records (
#     id INT PRIMARY KEY AUTO_INCREMENT,  # 审核记录ID，主键自增
#     inventory_id INT,  # 关联的库存记录ID
#     auditor_id INT,  # 审核员ID
#     status ENUM('approved','rejected'),  # 审核结果(通过/拒绝)
#     comment TEXT,  # 审核意见
#     created_at DATETIME DEFAULT CURRENT_TIMESTAMP,  # 审核时间，默认当前时间
#     FOREIGN KEY (inventory_id) REFERENCES inventory(id),  # 外键关联库存表
#     FOREIGN KEY (auditor_id) REFERENCES admin_users(id)  # 外键关联管理员表
# );
from exts import db
from enum import Enum
from .user import AdminUser

class Warehouse(db.Model):
    """仓库数据模型"""
    __tablename__ = 'warehouses'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 仓库ID
    name = db.Column(db.String(100), nullable=False)
    # 仓库名称，必填
    location = db.Column(db.String(200))
    # 仓库位置，可选
    created_by = db.Column(db.Integer, db.ForeignKey('admin_users.id'))
    # 创建者ID，外键关联管理员表
    
    # 关系属性
    creator = db.relationship('AdminUser', backref='created_warehouses')
    # 创建者关联属性
    inventory_items = db.relationship('Inventory', backref='warehouse')
    # 仓库关联的库存记录
    
    def __repr__(self):
        return f'<Warehouse {self.name}>'

class Item(db.Model):
    """物品数据模型"""
    __tablename__ = 'items'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 物品ID
    name = db.Column(db.String(100), nullable=False)
    # 物品名称，必填
    description = db.Column(db.Text)
    # 物品详细描述，可选
    created_by = db.Column(db.Integer, db.ForeignKey('admin_users.id'))
    # 创建者ID，外键关联管理员表
    
    # 关系属性
    creator = db.relationship('AdminUser', backref='created_items')
    # 创建者关联属性
    inventory_records = db.relationship('Inventory', backref='item')
    # 物品关联的库存记录
    
    def __repr__(self):
        return f'<Item {self.name}>'

class InventoryStatus(Enum):
    PENDING = 'pending'
    # 待审核
    APPROVED = 'approved'
    # 已审核
    REJECTED = 'rejected'
    # 已拒绝

class Inventory(db.Model):
    """库存记录数据模型"""
    __tablename__ = 'inventory'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 记录ID
    warehouse_id = db.Column(db.Integer, db.ForeignKey('warehouses.id'))
    # 仓库ID，外键关联仓库表
    item_id = db.Column(db.Integer, db.ForeignKey('items.id'))
    # 物品ID，外键关联物品表
    quantity = db.Column(db.Integer, nullable=False)
    # 物品数量，必填
    status = db.Column(db.Enum(InventoryStatus), default=InventoryStatus.PENDING)
    # 审核状态，默认待审核
    created_by = db.Column(db.Integer, db.ForeignKey('admin_users.id'))
    # 创建者ID，外键关联管理员表
    created_at = db.Column(db.DateTime, server_default=db.func.now())
    # 创建时间，默认当前时间
    
    # 关系属性
    creator = db.relationship('AdminUser', backref='created_inventories')
    # 创建者关联属性
    audit_records = db.relationship('AuditRecord', backref='inventory')
    # 审核记录关联属性
    
    def __repr__(self):
        return f'<Inventory {self.id}>'

class AuditStatus(Enum):
    APPROVED = 'approved'
    # 已通过
    REJECTED = 'rejected'
    # 已拒绝

class AuditRecord(db.Model):
    """审核记录数据模型"""
    __tablename__ = 'audit_records'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 审核记录ID
    inventory_id = db.Column(db.Integer, db.ForeignKey('inventory.id'))
    # 关联的库存记录ID，外键关联库存表
    auditor_id = db.Column(db.Integer, db.ForeignKey('admin_users.id'))  # 审核人 ID
    # 审核员ID，外键关联管理员表
    status = db.Column(db.Enum(AuditStatus))
    # 审核结果(通过/拒绝)
    comment = db.Column(db.Text)
    # 审核意见
    created_at = db.Column(db.DateTime, server_default=db.func.now())
    # 审核时间，默认当前时间
    
    # 关系属性
    auditor = db.relationship('AdminUser', backref='audited_records')
    # 审核员关联属性
    
    def __repr__(self):
        return f'<AuditRecord {self.id}>'

